package com.study.ctripapollo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.ctripapollo.entity.User;
import com.study.ctripapollo.mapper.UserMapper;
import com.study.ctripapollo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author flwu
 * @since 2020-02-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserInfoById(Integer id) throws Exception{
        User userInfo = userMapper.selectById(id);
        return userInfo;
    }

    @Override
    public User getUserInfo(QueryWrapper queryWrapper) throws Exception{
        User userInfo = userMapper.selectOne(queryWrapper);
        return userInfo;
    }

    @Override
    public List<User> getUserList(QueryWrapper queryWrapper) throws Exception {
        List<User> userList = userMapper.selectList(queryWrapper);
        return userList;
    }

    @Override
    public IPage<User> getUserListByPage(Integer page, Integer pageSize, Map<String, String> params) throws Exception{
        IPage<User> iPage = null;
        Page userPage = new Page(page, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sex", Integer.parseInt(params.get("sex")));
        queryWrapper.likeRight(true, "name", params.get("name"));
        iPage = userMapper.selectPage(userPage, queryWrapper);
        return iPage;

    }

    @Override
    public Boolean deleteUser(Integer id) throws Exception{
        Boolean result = new Boolean(false);
        Integer delResult = userMapper.deleteById(id);
        if (delResult > 0) {
            result = true;
        }
        return result;
    }

    @Override
    public Boolean updateUser(User user, QueryWrapper<User> queryWrapper) throws Exception{
        Integer updateResult = userMapper.update(user,queryWrapper);
        if (updateResult > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Integer addUser(User user) throws Exception {
        Integer result = userMapper.insert(user);
        if (result > 0) {
            return user.getId();
        }
        return result;
    }
}
